package weaver.jobs.sinopec.cron;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.sinopec.utils.BatchDataMigrationUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年10月08日 09:30
 */
public class WeaBatchDataMigrationCronJob extends BaseCronJob {
    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            baseBean.writeLog(">>>>>WeaBatchDataMigrationCronJob>>>>>curdate=" + DateUtil.getCurrentDate());
            RecordSetDataSource rsd = new RecordSetDataSource("OLDOA");
            BatchDataMigrationUtil batchDataMigrationUtil = new BatchDataMigrationUtil();
            RecordSet rs = new RecordSet();
            int count = 0;
            rs.executeQuery("select id,oldwfformid,newwfformid,newtablename,newformid,newmodeid from uf_lcsjqyjlb where status=0");
            int counts = rs.getCounts();
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                int oldformid = Math.abs(Util.getIntValue(rs.getString("oldwfformid"), 0));
                String formid = Util.null2String(rs.getString("newwfformid"));
                String modetablename = Util.null2String(rs.getString("newtablename"));
                int newformid = Math.abs(Util.getIntValue(rs.getString("newformid"), 0));
                int newmodeid = Math.abs(Util.getIntValue(rs.getString("newmodeid"), 0));
                if (newformid > 0 && newmodeid > 0) {
                    String oldtablename = "formtable_main_" + oldformid;
                    List<String> detailtableList = batchDataMigrationUtil.getDetailtable(formid);
                    int successTotal = 0;
                    rsd.execute("select * from " + oldtablename + " where 1=1");
                    while (rsd.next()) {
                        String mainid = Util.null2String(rsd.getString("id"));
                        String requestid = Util.null2String(rsd.getString("requestid"));
                        Map<String, Object> mainDataMap = new HashMap<>();
                        //遍历所有字段
                        String[] colnames = rsd.getColumnName();
                        for (String name : colnames) {
                            name = name.toLowerCase();
                            String value = Util.null2String(rsd.getString(name));
                            if (!"".equals(value) && !"id".equals(name)) {
                                if (batchDataMigrationUtil.isUploadMainField(formid, name)) {
                                    value = batchDataMigrationUtil.getNewDocidById(value, 1);
                                }
                                if (batchDataMigrationUtil.isSelectMainField(formid, name)) {
                                    value = batchDataMigrationUtil.getSelectItem(formid, name, value);
                                }
                                mainDataMap.put(name, value);
                            }
                        }
                        Map<String,String> requestInfo = batchDataMigrationUtil.getRequestInfo(requestid);
                        mainDataMap.put("workflowid", requestInfo.get("workflowid"));
                        mainDataMap.put("requestname", requestInfo.get("requestname"));
                        int dataid = batchDataMigrationUtil.insertModelData(modetablename, mainDataMap);
                        int index = 1;
                        int detailTotal = detailtableList.size();
                        int detailSuccessTotal = 0;
                        for (String detailtablename : detailtableList) {
                            String olddetailtablename = oldtablename + "_" + detailtablename.split("_")[3];
                            String newdetailtablename = modetablename + "_" + detailtablename.split("_")[3];
                            boolean addDetailStatus = batchDataMigrationUtil.addModeDetailData(olddetailtablename, newdetailtablename, formid, mainid, dataid);
                            if (addDetailStatus) {
                                detailSuccessTotal++;
                            }
                            index++;
                        }
                        boolean addLogStatus = batchDataMigrationUtil.addModeRequestLogData(requestid, dataid, modetablename + "_dt" + index);
                        if (addLogStatus && detailSuccessTotal == detailTotal) {
                            successTotal++;
                        }
                    }
                    baseBean.writeLog(">>>>>同步流程数据>>>>>successTotal=" + successTotal + "</br>");
                    rs.executeUpdate("update uf_lcsjqyjlb set amount=?,status=1,executedate=? where id=?", successTotal, DateUtil.getCurrentDate(), id);
                    count++;

                }
            }
            baseBean.writeLog(">>>>>查询列表历史流程数量>>>>>counts=" + counts + ",successTotal=" + count);
        } catch (Exception e) {
            baseBean.writeLog("WeaBatchDataMigrationCronJob", e);
            e.printStackTrace();
        }

    }
}
